﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using System.Transactions;
using FriendsNearby.Services.Entities;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace FriendsNearby.Services.Tests
{
    [TestFixture]
    public class DataAccessFixture
    {
        TransactionScope testContextTransaction;
        DataAccess dataAccess;

        [SetUp]
        public void SetUp()
        {
            testContextTransaction = new TransactionScope();

            Database database = DatabaseFactory.CreateDatabase("FriendsNearbyDB");

            // Temporaly clear DB
            database.ExecuteNonQuery(System.Data.CommandType.Text, "DELETE FROM UserRegistry");
            database.ExecuteNonQuery(System.Data.CommandType.Text, "DELETE FROM UserPreferences");
            //
            dataAccess = new DataAccess();
        }

        [TearDown]
        public void TearDown()
        {
            testContextTransaction.Dispose();
        }

        [Test]
        public void ShouldReadStoredUserPreferences()
        {
            var original = new UserPreference()
            {
                FacebookUserId = "DataAccessFixture_1",
                VisibilityLevel = (int) VisibilityLevel.Everyone,
                NearbyRadiusKM = 50
            };

            dataAccess.UpdateUserPreferences(original);

            var stored = dataAccess.ReadUserPreferences("DataAccessFixture_1");
            Assert.AreEqual("DataAccessFixture_1", stored.FacebookUserId);
            Assert.AreEqual(50, stored.NearbyRadiusKM);
        }


        [Test]
        public void ShouldBeAbleToUpdateExistingUserPreferences()
        {
            var original = new UserPreference()
            {
                FacebookUserId = "DataAccessFixture_2",
                VisibilityLevel = (int) VisibilityLevel.Everyone,
                NearbyRadiusKM = 50
            };

            dataAccess.UpdateUserPreferences(original);

            var changed = new UserPreference()
            {
                FacebookUserId = "DataAccessFixture_2",
                VisibilityLevel = (int) VisibilityLevel.FriendsAndTheirFriends,
                NearbyRadiusKM = 100
            };

            dataAccess.UpdateUserPreferences(changed);

            var stored = dataAccess.ReadUserPreferences("DataAccessFixture_2");
            Assert.AreEqual("DataAccessFixture_2", stored.FacebookUserId);
            Assert.AreEqual(100, stored.NearbyRadiusKM);
        }

        [Test]
        public void ShouldCreateANonExistingUser()
        {
            dataAccess.EnsureUserExists("TestUser1", "123456");
            Assert.AreEqual(dataAccess.GetUserSessionKey("TestUser1"), "123456");
        }

        [Test]
        public void ShouldChangeTheSessionKeyOfAnExistingUser()
        {
            dataAccess.EnsureUserExists("TestUser1", "123456");
            dataAccess.EnsureUserExists("TestUser1", "123");
            Assert.AreEqual(dataAccess.GetUserSessionKey("TestUser1"), "123");
        }

        [Test]
        public void ShouldChangeTheSessionKeyOfAnExistingUserWithDifferentDataAccess()
        {
            DataAccess otherDataAccess = new DataAccess();
            dataAccess.EnsureUserExists("TestUser1", "123456");
            otherDataAccess.EnsureUserExists("TestUser1", "123");
            Assert.AreEqual(dataAccess.GetUserSessionKey("TestUser1"), "123");
        }
    }
}
